package com.caseProject.puremvc.mediator.gui.simple {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	
	public class SimpleGraphic extends Sprite {
		
		private var view:Sprite;
		
		private var _w:Number;
		private var _h:Number;
		
		private var field:TextField;
				
		private var _text:String = "";
		
		public function SimpleGraphic(view:Sprite) {
			this.view = view;
			this.field = new TextField ();
			
			this.mouseChildren = false;			
			this.addEventListener (Event.ADDED_TO_STAGE, onInit);
		}

		public function get h():Number
		{
			return _h;
		}

		public function set h(value:Number):void
		{
			_h = value;
		}

		public function get w():Number
		{
			return _w;
		}

		public function set w(value:Number):void
		{
			_w = value;
		}

		private function onInit(e:Event):void {
			removeEventListener(Event.ADDED_TO_STAGE, onInit);
			
			this.field.border = false;
			this.field.borderColor = 0xFF0000;
			this.field.selectable = false;
			this.field.multiline = true;
			this.field.autoSize = TextFieldAutoSize.LEFT;
			this.field.type = TextFieldType.DYNAMIC;
			this.field.wordWrap = true;
			
			var format:TextFormat = new TextFormat ();
			format.color = 0x000000;
			format.size = 14;
			format.font = "Arial";
			this.field.defaultTextFormat = format;
			this.field.setTextFormat (format);
			
			this.addChild (this.view);
			this.addChild (this.field);
			
			this.redraw (100,50);
			
			this.doubleClickEnabled = true;
		}
				
		public function redraw(w:Number = 100, h:Number = 50):void {
			
			if (w < this.field.width + 25) {
				w = this.field.width + 25;
			}
			if (h < this.field.height + 25) {
				h = this.field.height + 25;
			}
			
			this.w = w;
			this.h = h;
						
			this.view.width = w;
			this.view.height = h;
			
			this.view.x = -w/2;
			this.view.y = -h/2;
						
			this.field.x = this.view.x + 15;
			this.field.y = this.view.y + 15;
			this.field.width = this.view.width - 30;
			this.field.height = this.view.height - 30;
		}
		
		public function get text():String {
			return _text;
		}
		
		public function set text(value:String):void {
			_text = value;
			this.field.visible = (value != "");
			this.field.text = _text;
			this.redraw ();
		}		
	}
}